<?php

class DMail{
	private static function EscapeHead($string, $encoding = 'GB2312'){
		$string	= mb_convert_encoding($string, $encoding, "UTF-8");
		return '=?' . $encoding . '?B?'. base64_encode($string) .'?=';
	}

	private static function EscapePart($string, $encoding = 'GB2312'){
		$string = mb_convert_encoding($string, $encoding, 'UTF-8');
		return preg_replace_callback( '/([\x80-\xFF]+.*[\x80-\xFF]+)/' ,create_function ( '$m' ,"return \"=?$encoding?B?\".base64_encode(\$m[1]).\"?=\";") ,$string);
	}

	public static function SMTP($from = '', $to = '', $subject = '', $message = '', $options = null, $bcc = array()){
		$options['encoding'] = $options['encoding'] ? $options['encoding'] : 'UTF-8';
		$options['contentType'] = $options['contentType'] ? $options['contentType'] : 'text/html';
//		$message = mb_convert_encoding($message, $options['encoding'], 'UTF-8');

		$host = 'smtp.exmail.qq.com';
		$port = 465;
		$ssl = 'ssl';
		$user = 'yangkun@heivr.com';
		$pass = 'Admin123';
		$from = $from ? $from : 'yinna@heivr.com';
		$reply = $reply ? $reply : 'yinna@heivr.com';
		$site = 'Heivr Tech';

		$message .= "\n<br /><br />". '来自<strong><span style="color:black;">黑</span><span style="color:red;">晶</span></strong>OA系统的邮件';

		$subject = self::EscapeHead($subject, $options['encoding']);
		$site = self::EscapeHead($site, $options['encoding']);
		$body = $message;

		$ishtml = ($options['contentType'] == 'text/html');
		//begin
		$mail = new PHPMailer();
		$mail->IsSMTP(); 
		$mail->CharSet = $options['encoding'];
		$mail->SMTPAuth   = true; 
		$mail->Host = $host;
		$mail->Port = $port;
		if ( $ssl == 'ssl' ) {
			$mail->SMTPSecure = "ssl"; 
		} else if ( $ssl == 'tls' ) {
			$mail->SMTPSecure = 'tls'; 
		}
		$mail->Username = $user;
		$mail->Password = $pass;
		$mail->SetFrom($from, $site);
		$mail->AddReplyTo($reply, $site);
		foreach($bcc as $bo) {
			$mail->AddBCC($bo);
		}
		$mail->Subject = $subject;
		if ( $ishtml ) {
			$mail->MsgHTML($body);
		} else {
			$mail->Body = $body;
		}
		$mail->AddAddress($to);
		return $mail->Send();
	}
}

?>